package com.chenfan.process.entity.po;

import com.baomidou.mybatisplus.annotation.TableName;
import com.chenfan.process.enums.OperationEnum;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

/**
 * @author liuming
 * @date 2021/11/24
 */
@Data
@TableName(value = "process_related_attributes")
public class ProcessRelatedAttribute {

    private Long id;

    /**
     * 相关id
     */
    private Long relatedId;

    /**
     * 关联类型
     */
    private Integer relatedType;
    /**
     * 组id
     */
    private Long groupId;
    /**
     * 关联名字
     */
    private String relatedName;

    /**
     * 审批模板id
     */
    private Long approvalTemplateId;

    /**
     * 表单属性id
     */
    private Long formAttributeId;

    /**
     * 表单属性类型 文本, 数字等
     */
    private Integer formAttributeType;

    /**
     * 关联值
     */
    private String relatedValue;

    // 解决前端传参把字符串也转换成""的问题
    public String getRelatedValue() {
        if (relatedValue.startsWith("[")) {
            // 表示为json数组格式的数据, 不处理. 按原始格式处理.
            return relatedValue;
        }
        return StringUtils.isEmpty(relatedValue) ? relatedValue : relatedValue.replace("\"", "");
    }

    /**
     * 关联Key
     */
    private String relatedKey;

    /**
     * 关联关系
     */
    private Integer relatedRelation;

    /**
     * 版本
     */
    private Long version;

    public boolean and() {
        return OperationEnum.AND.getCode().equals(getRelatedRelation());
    }

    public boolean or() {
        return OperationEnum.OR.getCode().equals(getRelatedRelation());
    }

    @Data
    public static class Constants {
        public static final String RELATED_ID = "related_id";
        public static final String FORM_ATTRIBUTE_ID = "form_attribute_id";
        public static final String APPROVAL_TEMPLATE_ID = "approval_template_id";

        private Constants() {}
    }
}
